package org.transformatorhuis.unix.server;

import java.io.*;

/**
 * User: Olivier
 * Date: 31-Mar-2010
 */
public class ServerFactory {

    public static Server createInstance(String[] args, Cloneable processThread) throws Exception {
        if(isCloneable(processThread))
            return new CloneableServer(args, processThread);
        else
            throw new AbstractServer.RTFMException("Not cloneable");
    }

    public static Server createInstance(String[] args, Serializable processThread) throws Exception {
        if(isSerializable(processThread))
            return new SerializableServer(args, processThread);
        else
            throw new AbstractServer.RTFMException("Not serialazable");
    }

    /**
     * An object is serializable if the following criteria are met:
     * - object implements Serializable interface
     * - is equal (using the equals method) to a serialized copy of itself
     * - has the same hashcode as a serialized copy of itself
     * Recommendations:
     * - Read this: http://oreilly.com/catalog/javarmi/chapter/ch10.html
     * <p/>
     * This method is static so it can easily be used in a unit test
     *
     * @param objectToSerialize object to serialize
     * @return true if all criteria mentioned above are met
     */
    public static boolean isSerializable(Object objectToSerialize) {
        if (!(objectToSerialize instanceof Serializable))
            return false;
        ByteArrayOutputStream bao = new ByteArrayOutputStream();
        try {
            ObjectOutputStream oos = new ObjectOutputStream(bao);
            oos.writeObject(objectToSerialize);
            byte[] ob = bao.toByteArray();
            ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(ob));
            Object check = ois.readObject();
            if (!check.equals(objectToSerialize))
                return false;
            if (check.hashCode() != objectToSerialize.hashCode())
                return false;
        } catch (IOException e) {
            return false;
        } catch (ClassNotFoundException e) {
            return false;
        }
        return true;
    }

    /**
     * An object is cloneable if the following criteria are met:
     * - implement Cloneable interface
     * - implements public clone method (this is _not_ enforced by the cloneable interface)
     * Recommendation for implementing clone in you objects:
     * - read this: http://www.javaworld.com/javaworld/javaqa/2003-01/02-qa-0124-clone.html
     * - be aware that threads can share the same references to object, if you don't want this do a
     * deep copy (All collection classes do deep copy and implement their own (public) clone method)
     * - if you implement clone go through the object hierarchy by doing super.clone()
     * - it helps if you understand pointers (something Java tries to hide from you)
     * - use serializable instead of clone.
     * <p/>
     * This method is static so it can easily be used in a unit test
     *
     * @param objectToClone
     * @return
     */
    public static boolean isCloneable(Object objectToClone) {
        if (objectToClone instanceof Cloneable) {
            Class clazz = objectToClone.getClass();
            try {
                clazz.getMethod("clone", null);
            } catch (NoSuchMethodException e) {
                return false;
            }
            return true;
        }
        return false;
    }

}
